package cn.edu.scau.cmi.ema.controller;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.base.CmiEmaController;
import cn.edu.scau.cmi.ema.domain.Address;
import cn.edu.scau.cmi.ema.domain.Food;
import cn.edu.scau.cmi.ema.domain.Foodsaleaddress;
import cn.edu.scau.cmi.ema.domain.Unit;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;

@Controller("FoodsaleaddressController")
public class FoodsaleaddressController extends CmiEmaController {

	@RequestMapping("/listFoodsaleaddresss")
	public ModelAndView listFoodsaleaddresss(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = foodsaleaddressPaginationService.updatePagination(request, pagination);
		Set<Foodsaleaddress> foodsaleaddresss = foodsaleaddressPaginationService
				.getPaginationFoodsaleaddresss(pagination);

		mav.addObject("foodsaleaddresss", foodsaleaddresss);
		mav.addObject("entityName", "foodsaleaddress");
		mav.addObject("pagination", pagination);
		mav.setViewName("foodsaleaddress/listFoodsaleaddresss");
		return mav;
	}

	public String listFoodsaleaddresss() {
		return "redirect:/listFoodsaleaddresss";
	}

	@RequestMapping("/viewFoodsaleaddress")
	public ModelAndView viewFoodsaleaddress(@RequestParam Integer foodsaleaddressId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("foodsaleaddress", foodsaleaddressDAO.findFoodsaleaddressByPrimaryKey(foodsaleaddressId));
		mav.setViewName("foodsaleaddress/viewFoodsaleaddress");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteFoodsaleaddresss")
	public ModelAndView confirmDeleteFoodsaleaddresss(
			@RequestParam(value = "deleteFoodsaleaddresssIds", required = true) Set<Integer> deleteFoodsaleaddresssIds) {
		ModelAndView mav = new ModelAndView();

		Set<Foodsaleaddress> deleteFoodsaleaddresss = foodsaleaddressDAO
				.findFoodsaleaddresssByIds(deleteFoodsaleaddresssIds);
		Set<Foodsaleaddress> referencedFoodsaleaddresss = foodsaleaddressService
				.loadReferenceFoodsaleaddresss(deleteFoodsaleaddresss);
		boolean hasReferenced;
		if (referencedFoodsaleaddresss.size() == 0) {
			mav.addObject("deleteEntitys", deleteFoodsaleaddresss);
			hasReferenced = false;
		} else {
			deleteFoodsaleaddresss.removeAll(referencedFoodsaleaddresss);
			mav.addObject("referencedEntitys", referencedFoodsaleaddresss);
			mav.addObject("deleteEntitys", deleteFoodsaleaddresss);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Foodsaleaddress");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listFoodsaleaddresss");
		return mav;
	}

	@RequestMapping("/deleteFoodsaleaddress")
	public String deleteFoodsaleaddress(@RequestParam Integer foodsaleaddressId) {
		Foodsaleaddress foodsaleaddress = foodsaleaddressDAO.findFoodsaleaddressByPrimaryKey(foodsaleaddressId);
		foodsaleaddressService.deleteFoodsaleaddress(foodsaleaddress);
		return "forward:/listFoodsaleaddresss";
	}

	@RequestMapping("/confirmDeleteFoodsaleaddress")
	public ModelAndView confirmDeleteFoodsaleaddress(@RequestParam Integer foodsaleaddressId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("foodsaleaddress", foodsaleaddressDAO.findFoodsaleaddressByPrimaryKey(foodsaleaddressId));

		mav.setViewName("foodsaleaddress/deleteFoodsaleaddress");

		return mav;
	}

	@RequestMapping("/deleteFoodsaleaddresss")
	public String deleteFoodsaleaddresss(
			@RequestParam(value = "deleteFoodsaleaddresssId", required = true) Set<Integer> deleteFoodsaleaddresssId) {
		foodsaleaddressService.deleteFoodsaleaddresss(deleteFoodsaleaddresssId);
		return "forward:/listFoodsaleaddresss";
	}

	@RequestMapping("/editFoodsaleaddress")
	public ModelAndView editFoodsaleaddress(@RequestParam Integer foodsaleaddressId) {

		ModelAndView mav = new ModelAndView();

		Foodsaleaddress foodsaleaddress = foodsaleaddressDAO.findFoodsaleaddressByPrimaryKey(foodsaleaddressId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("foodsaleaddress", foodsaleaddress);

		// 把所有的引用foodsaleaddress对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(foodsaleaddressService.loadFoodsaleaddressObjectReferencedSet(foodsaleaddress));

		// 将编辑页面foodsaleaddress添加到mav中
		mav.setViewName("foodsaleaddress/editFoodsaleaddress");
		return mav;
	}

	@RequestMapping("/newFoodsaleaddress")
	public ModelAndView newFoodsaleaddress() {
		ModelAndView mav = new ModelAndView();
		Foodsaleaddress foodsaleaddress = new Foodsaleaddress();

		mav.addObject("foodsaleaddress", foodsaleaddress);

		// 把所有的引用foodsaleaddress对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(foodsaleaddressService.loadFoodsaleaddressObjectReferencedSet(foodsaleaddress));

		mav.addObject("newFlag", true);
		mav.setViewName("foodsaleaddress/editFoodsaleaddress");

		return mav;
	}

	@RequestMapping("/saveFoodsaleaddress")
	public ModelAndView saveFoodsaleaddress(@ModelAttribute Foodsaleaddress foodsaleaddress) {

		ModelAndView mav = new ModelAndView();

		Address addressTemp = foodsaleaddress.getAddress();
		Address address = addressTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(addressDAO.findAddressById(addressTemp.getId()));
		Food foodTemp = foodsaleaddress.getFood();
		Food food = foodTemp == null ? null : CmiHibernateProxyUtil.deproxy(foodDAO.findFoodById(foodTemp.getId()));
		Unit unitTemp = foodsaleaddress.getUnit();
		Unit unit = unitTemp == null ? null : CmiHibernateProxyUtil.deproxy(unitDAO.findUnitById(unitTemp.getId()));

		Foodsaleaddress existingFoodsaleaddress = CmiHibernateProxyUtil.deproxy(foodsaleaddressDAO
				.findFoodsaleaddressByPhysicalPrimaryKey(food.getId(), address.getId(), foodsaleaddress.getDate()));

		foodsaleaddress.setAddress(address);
		foodsaleaddress.setFood(food);
		foodsaleaddress.setUnit(unit);
		// 新建记录
		if (foodsaleaddress.getId() == null) {
			if (foodsaleaddress.equals(existingFoodsaleaddress)) {

				mav.addAllObjects(foodsaleaddressService.loadFoodsaleaddressClassReferencedSet(foodsaleaddress));
				// mav = editContinue(mav, foodsaleaddress);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("foodsaleaddress/editFoodsaleaddress");
			} else {
				foodsaleaddressService.saveFoodsaleaddress(foodsaleaddress);
				mav.setViewName("forward:/listFoodsaleaddresss");
			}
		}
		// 编辑记录
		else {
			Foodsaleaddress originFoodsaleaddress = CmiHibernateProxyUtil
					.deproxy(foodsaleaddressDAO.findFoodsaleaddressByPrimaryKey(foodsaleaddress.getId()));
			if (!foodsaleaddress.equals(originFoodsaleaddress) && foodsaleaddress.equals(existingFoodsaleaddress)) {
				mav.addAllObjects(foodsaleaddressService.loadFoodsaleaddressClassReferencedSet(foodsaleaddress));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("foodsaleaddress/editFoodsaleaddress");
			} else {
				foodsaleaddressService.saveFoodsaleaddress(foodsaleaddress);
				mav.setViewName("forward:/listFoodsaleaddresss");
			}
		}
		return mav;
	}

	@RequestMapping("/foodsaleaddressController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
